﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.Framework.DataClientDBAgent;
using HF.Framework.Web;
using HF.WorkFlow.Const;
using HF.Framework.SqlDataContract;
public partial class LinkManEdit : System.Web.UI.Page
{
    string userid = "";
    string state = "";
    string addtype = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["State"] != null)
            state = Request["State"].ToString();
        else
            state = WorkFlowConst.STATE_VIEW;
        foreach (Control cl in this.Controls)
        {
            setReadOnly(cl);
        }
        if (Request["AddType"] != null)
        {
            addtype = Request["AddType"].ToString();
            tbxAddType.Text = addtype;
        }

        if (Request["UserId"] != null)
        {
            userid=Request["UserId"].ToString();
            if (!this.IsPostBack)
            {
                string sql = "select * from OA_Address where taskid=@taskid";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sql;
                sqlItem.AppendParameter("@taskid", userid);
                ClientDBAgent agent = new ClientDBAgent();
                DataTable dt = agent.ExecuteDataTable(sqlItem);

                if (dt != null && dt.Rows.Count > 0)
                {
                    tbxName.Text = dt.Rows[0]["Name"].ToString();
                    tbxCompany.Text = dt.Rows[0]["Company"].ToString();
                    tbxAddress.Text = dt.Rows[0]["Address"].ToString();
                    tbxTel.Text = dt.Rows[0]["tel"].ToString();
                    tbxMbtel.Text = dt.Rows[0]["Mbtel"].ToString();
                    tbxFax.Text=dt.Rows[0]["Fax"].ToString();
                    tbxEmail.Text = dt.Rows[0]["email"].ToString();
                    tbxZipcode.Text = dt.Rows[0]["Zipcode"].ToString();
                    tbxQQ.Text = dt.Rows[0]["qq"].ToString();
                    tbxMsn.Text = dt.Rows[0]["msn"].ToString();
                    tbxRemark.Text = dt.Rows[0]["Remark"].ToString();
                    tbxAccount.Text = dt.Rows[0]["Account"].ToString();
                    tbxAddType.Text = dt.Rows[0]["AddType"].ToString();
                }
            }
        }
    }
    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
        if (state == WorkFlowConst.STATE_ADD)
        {

            string sql = "insert into OA_Address(taskid,Name,Company,Address,tel,Mbtel,email,Zipcode,qq,msn,Remark,Account,AddType,Fax)" +
                " values(@taskid,@Name,@Company,@Address,@tel,@Mbtel,@email,@Zipcode,@qq,@msn,@Remark,@Account,@AddType,@Fax)";
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.AppendParameter("@Name", tbxName.Text);
            sqlItem.AppendParameter("@Company", tbxCompany.Text);
            sqlItem.AppendParameter("@Address", tbxAddress.Text);
            sqlItem.AppendParameter("@tel", tbxTel.Text);
            sqlItem.AppendParameter("@Mbtel", tbxMbtel.Text);
            sqlItem.AppendParameter("@email", tbxEmail.Text);
            sqlItem.AppendParameter("@Zipcode", tbxZipcode.Text);
            sqlItem.AppendParameter("@qq", tbxQQ.Text);
            sqlItem.AppendParameter("@msn", tbxMsn.Text);
            sqlItem.AppendParameter("@Remark", tbxRemark.Text);
            sqlItem.AppendParameter("@Account", this.User.Identity.Name);
            sqlItem.AppendParameter("@AddType", tbxAddType.Text);
            sqlItem.AppendParameter("@Fax", tbxFax.Text);
            sqlItem.AppendParameter("@taskid", Guid.NewGuid().ToString());
            sqlItem.CommandText = sql;
            ClientDBAgent agent = new ClientDBAgent();
            agent.ExecuteNonQuery(sqlItem);


        }
        else
        {
            string sql = "update  OA_Address set Name=@Name,Company=@Company,Address=@Address,tel=@tel,Mbtel=@Mbtel,Fax=@Fax,"+
                         "email=@email,Zipcode=@Zipcode,qq=@qq,msn=@msn,Remark=@Remark,Account=@Account,AddType=@AddType" +
                           " where taskid='"+userid+"'";
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.AppendParameter("@Name", tbxName.Text);
            sqlItem.AppendParameter("@Company", tbxCompany.Text);
            sqlItem.AppendParameter("@Address", tbxAddress.Text);
            sqlItem.AppendParameter("@tel", tbxTel.Text);
            sqlItem.AppendParameter("@Mbtel", tbxMbtel.Text);
            sqlItem.AppendParameter("@email", tbxEmail.Text);
            sqlItem.AppendParameter("@Zipcode", tbxZipcode.Text);
            sqlItem.AppendParameter("@qq", tbxQQ.Text);
            sqlItem.AppendParameter("@msn", tbxMsn.Text);
            sqlItem.AppendParameter("@Remark", tbxRemark.Text);
            sqlItem.AppendParameter("@Account", tbxAccount.Text);
            sqlItem.AppendParameter("@AddType", tbxAddType.Text);
            sqlItem.AppendParameter("@Fax", tbxFax.Text);
            sqlItem.CommandText = sql;
            ClientDBAgent agent = new ClientDBAgent();
            agent.ExecuteNonQuery(sqlItem);
        }
        WorkDialog.ShowMessage(this,"保存成功!");
        btnSave.Enabled = false;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.close()</script>");
    }

    public void setReadOnly(Control crl)
    {
        if (state == WorkFlowConst.STATE_VIEW)
        {
            string ctrlTypeName = "";
            foreach (Control c in crl.Controls)
            {
                ctrlTypeName = c.GetType().Name;
                switch (ctrlTypeName)
                {
                    case "Button":
                        (c as Button).Enabled = false;
                        break;
                    case "DrapDownList":
                        (c as Button).Enabled = false;
                        break;
                    case "TextBox":
                        (c as TextBox).ReadOnly = true;
                        break;
                }
                if (c.HasControls())
                    setReadOnly(c);
            }
        }
    }

}
